<template>
  <div>
    <el-dialog
      :title="dialogtitle"
      width="1080px"
      class="icon-dialog"
      :visible.sync="show"
      :before-close="closeForm"
      append-to-body
      @open="open"
    >
      <el-form
        ref="form"
        :size="size"
        :model="form"
        :rules="rules"
        :label-width="$store.getters.device !== 'mobile'?'16%':'90px'"
      >
      <el-row>
        <el-col :span="24">
          <el-form-item label="奖品">
            <el-radio-group v-model="form.ptype">
              <el-radio v-for="(item,i) in ptypearray" :key="i" :label="item.val">{{ item.key }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="标题" prop="title">
            <el-input v-model="form.title" auto-complete="off" clearable placeholder="请输入标题" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="图片">
            <ImagesUpload size="mini" file-type="image" :image.sync="form.image" />
          </el-form-item>
        </el-col>
      </el-row>
        <el-row v-if="form.ptype==2">
          <el-col :span="24">
            <el-form-item label="红包金额" prop="price">
              <el-input v-model="form.price" style="width:300px;" auto-complete="off" clearable placeholder="请输入红包金额">
                <template slot="append">元</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="form.ptype==3">
          <el-col :span="24">
            <el-form-item label="优惠券">
              <el-select v-model="form.coupon_id" style="width:100%" filterable clearable placeholder="请选择">
                <el-option v-for="(item,i) in couponarray" :key="i" :label="item.key" :value="item.val" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="form.ptype==4">
          <el-col :span="24">
            <el-form-item :label="lang.points+'数量'" prop="points">
              <el-input-number
                v-model="form.points"
                controls-position="right"
                style="width:300px;"
                auto-complete="off"
                clearable
                :min="0"
                :placeholder="'请输入'+lang.points+'数量'"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="状态">
              <el-switch v-model="form.status" :active-value="1" :inactive-value="0" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="排序">
              <el-input-number
                v-model="form.sort"
                controls-position="right"
                style="width:300px;"
                auto-complete="off"
                clearable
                :min="0"
                placeholder="排序"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="loading" type="primary" @click="submit">
          <span v-if="!loading">保 存</span>
          <span v-else>提 交 中...</span>
        </el-button>
        <el-button @click="closeForm">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import ImagesUpload from '@/components/common/ImagesUpload.vue'
export default {
  name: 'rotarytableprizeupdate',
  components: {ImagesUpload},
  props: {
    show: {
      type: Boolean,
      default: false
    },
    opentype: {
      type: String,
      default: 'add'
    },
    size: {
      type: String,
      default: 'small'
    },
    info: {
      type: Object
    }
  },
  data() {
    return {
      lang: {},
      dialogtitle: '',
      form: {
        title: '',
        status: 1
      },
      ptypearray: [],
      couponarray:[],
      loading: false,
      rules: {
        title: [{
          required: true,
          message: '请填写标题',
          trigger: 'blur'
        }]
      }
    }
  },
  mounted() {
    this.$api.post('/Lang/getlang').then(res => {
      this.lang = res.data
    })
  },
  watch: {
    show(val) {
      if (val) {
        this.$api.post('/rotarytableprize/getField').then(res => {
          this.ptypearray = res.data.ptypearray;
          this.couponarray = res.data.couponarray;
        })
      }
    }
  },
  methods: {
    open() {
      if (this.opentype == 'update') {
        this.dialogtitle = '修改'
        this.form = this.info
      } else {
        this.dialogtitle = '添加'
        this.form = {
          sort: 100,
          goods_id: 0,
          goods: [],
          status: 1
        }
        // console.log(this.form);
      }
    },
    submit() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loading = true
          this.$api.post('/rotarytableprize/update', this.form).then(res => {
            this.$message({
              message: res.msg,
              type: 'success'
            })
            this.$emit('refesh_list')
            this.closeForm()
          }).catch(() => {
            this.loading = false
          })
        }
      })
    },
    closeForm() {
      this.$emit('update:show', false)
      this.loading = false
      if (this.$refs['form'] !== undefined) {
        this.$refs['form'].resetFields()
      }
    }
  }
}
</script>
<style scoped>
  .goods_pic {
    width: 100px;
    vertical-align: middle;
  }
</style>
